package com.leetcode.No1742;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @program: Solutin
 * @description: 盒子中小球的最大数量
 * @author: Wang Zhihua
 * @date: 2022-11-24
 */
public class Solutin {

    public int countBalls(int lowLimit, int highLimit) {

        Map<Integer, Integer> numMap = new HashMap<>();

        for (int i = lowLimit; i <= highLimit; ++i) {
            int tmpNum = i;
            int id = 0;
            while (tmpNum != 0) {
                id += (tmpNum % 10);
                tmpNum /= 10;
            }
            Integer sum = numMap.get(id);
            numMap.put(id, (Objects.isNull(sum) ? 1 : sum + 1));
        }

        int ans = 0;
        for (Integer sum : numMap.values()) {
            ans = Math.max(ans, sum);
        }

        return ans;
    }

    @Test
    public void test01() {
        int lowLimit, highLimit;

        lowLimit = 1;
        highLimit = 10;
        System.out.println(countBalls(lowLimit, highLimit));

        lowLimit = 5;
        highLimit = 15;
        System.out.println(countBalls(lowLimit, highLimit));
    }
}
